﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SinaApi
{
    public class SinaBlogApiService : ISinaBlogService
    {
        //
        private readonly string[] _sinaAppKey = { "3270607952", "2351187680", "3973039044" };

        private readonly Random _random = new Random((int)DateTime.Now.Ticks);

        private readonly string _userName;
        private readonly string _password;

        public static Dictionary<string, object> Cache = new Dictionary<string, object>();

        public SinaBlogApiService():this("ygd.weibo.superwolf@gmail.com", "superwolf")
        {
        }


        public SinaBlogApiService(string userName, string password)
        {
            this._userName = userName;
            this._password = password;
        }

        private string GetRandomSinaAppKey()
        {
            return _sinaAppKey[_random.Next(_sinaAppKey.Count()-1)];
        }

        #region ISinaBlogService

        public string GetFollowings()
        {
            var apiHelper = new SinaBlogApiHelper(this._userName, this._password);
            string url = string.Format("http://api.t.sina.com.cn/statuses/friends.json?source={0}", GetRandomSinaAppKey());
            string data = apiHelper.Get(url);
            return data;
        }


        public List<Status> GetStatuses()
        {
            var statuses = new List<Status>();
            var apiHelper = new SinaBlogApiHelper(this._userName, this._password);
            string url = string.Format("http://api.t.sina.com.cn/statuses/mentions.xml?source={0}", GetRandomSinaAppKey());
            string data = apiHelper.Get(url);
            var statusCollection = XmlSerializeHelper.Deserialize<StatusCollection>(data, Encoding.UTF8);

            return statusCollection.items;
        }

        public Status ToUpdate(string twiiterContent)
        {
            var apiHelper = new SinaBlogApiHelper(_userName, _password);

            string url = string.Format("http://api.t.sina.com.cn/statuses/update.xml?source={0}", GetRandomSinaAppKey());
            string param = string.Format("status={0}&annotations={1}", twiiterContent, "[{\"type2\":123}]");
            string statusString = apiHelper.Post(url, param);
            var status = XmlSerializeHelper.Deserialize<Status>(statusString,Encoding.UTF8);
            return status;
        }

        public void ToComment(long id, string comment)
        {
            var apiHelper = new SinaBlogApiHelper(_userName, _password);
            
            string url = string.Format("http://api.t.sina.com.cn/statuses/comment.xml?source={0}", GetRandomSinaAppKey());
            string param = string.Format("id={0}&comment={1}", id, comment);
            apiHelper.Post(url, param);
        }

        #region useless
        public List<Status> GetNewStatuses()
        {
            List<Status> statuses = new List<Status>();
            SinaBlogApiHelper apiHelper = new SinaBlogApiHelper();
            string url = string.Format("http://api.t.sina.com.cn/statuses/mentions.xml?source={0}", GetRandomSinaAppKey());
            string data = apiHelper.Get(url, this._userName, this._password);
            StatusCollection statusCollection = XmlSerializeHelper.Deserialize<StatusCollection>(data, Encoding.UTF8);
            statusCollection.items.ForEach(status =>
            {
                if (!Cache.ContainsKey("all_status_" + status.id))
                {
                    Cache.Add("all_status_" + status.id, status);
                    Cache.Add("uncomment_status_" + status.id, status);
                    statuses.Add(status);
                }
            });

            return statuses;
        }

        public bool Comment(long id, string comment)
        {
            bool result = false;
            string url = string.Format("http://api.t.sina.com.cn/statuses/comment.xml?source={0}", GetRandomSinaAppKey());
            string param = string.Format("id={0}&comment={1}", id, comment);
            try
            {
                SinaBlogApiHelper apiHelper = new SinaBlogApiHelper();
                apiHelper.Post(url, param, _userName, _password);
                result = true;
                if (Cache.ContainsKey("uncomment_status_" + id))
                {
                    Cache.Remove("uncomment_status_" + id);
                }
            }
            catch { }
            return result;
        }


        public List<Status> GetALLStatuses()
        {
            List<Status> statuses = new List<Status>();
            Cache.Where(c => c.Key.Contains("all_status")).Select(p => p.Value).ToList().ForEach(status =>
            {

                statuses.Add((Status)status);
            });
            return statuses;
        }

        public List<Status> GetUncommetStatuses()
        {
            List<Status> statuses = new List<Status>();
            Cache.Where(c => c.Key.Contains("uncomment_status")).Select(p => p.Value).ToList().ForEach(status =>
            {

                statuses.Add((Status)status);
            });
            return statuses;
        }

        #endregion useless
        #endregion
    }
}
